<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/4/18
 * Time: 20:11
 */
namespace app\index\controller;

use app\common\controller\Frontend;

/**
 * 博客导航控制器
 * Class Navigation
 * @package app\index\controller
 */
class Navigation extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $pageInfo=db('navigation')->where('controllername="'.$this->identify.'"')->field('id,title,keywords,description')->find();
        if(empty($pageInfo)) $this->error('导航不存在！',url('/index'));

        //banner
        $bannerList=db('navigation_banner')->order('sort desc,id asc')->field('title,thumb,link')->select();

        //默认取博客顶级分类中的第一个
        $cid=!empty($this->request->param('cid'))?intval($this->request->param('cid')):db('navigation_category')->where('pid=0')->order('sort desc,id asc')->limit(1)->value('id');

        //查询当前博客分类
        $category=db('navigation_category')->where('pid=0 and id='.$cid)->order('sort desc,id asc')->field('id,name')->find();
        if(!empty($category)){
            $category['childCategoryList']=db('navigation_category')->where('pid='.$category['id'])->field('id,name,title')->order('sort desc,id asc')->select();
            if(!empty($category['childCategoryList'])){
                foreach ($category['childCategoryList'] as &$ccl_v){
                    $ccl_v['linkList']=db('navigation_link')->where('category_id='.$ccl_v['id'])->order('sort desc,id desc')->field('id,name,link')->select();
                }
                unset($ccl_v);
            }
        }

        //查询所有顶级博客分类
        $categoryList=db('navigation_category')->where('pid=0')->order('sort desc,id asc')->field('id,name')->select();

        $this->assign('pageInfo',$pageInfo);
        $this->assign('bannerList',$bannerList);
        $this->assign('category',$category);
        $this->assign('categoryList',$categoryList);
        return $this->view->fetch();
    }

}